/**
 * cron表达式 - 编辑页面
 * @author yuan_rz
 * @date 2019/03/11 18:03
 */
define(function () {

    var self,
        language = {
            zh_CN: {
                Seconds: {
                    name: '秒',
                    every: '每一秒钟',
                    interval: ['每隔', '秒执行 从', '秒开始'],
                    specific: '具体秒数(可多选)',
                    cycle: ['周期从', '到', '秒']
                },
                Minutes: {
                    name: '分',
                    every: '每一分钟',
                    interval: ['每隔', '分执行 从', '分开始'],
                    specific: '具体分钟数(可多选)',
                    cycle: ['周期从', '到', '分']
                },
                Hours: {
                    name: '时',
                    every: '每一小时',
                    interval: ['每隔', '小时执行 从', '小时开始'],
                    specific: '具体小时数(可多选)',
                    cycle: ['周期从', '到', '小时']
                },
                Day: {
                    name: '天',
                    every: '每一天',
                    intervalWeek: ['每隔', '周执行 从', '开始'],
                    intervalDay: ['每隔', '天执行 从', '天开始'],
                    specificWeek: '具体星期几(可多选)',
                    specificDay: '具体天数(可多选)',
                    lastDay: '在这个月的最后一天',
                    lastWeekday: '在这个月的最后一个工作日',
                    lastWeek: ['在这个月的最后一个'],
                    beforeEndMonth: ['在本月底前', '天'],
                    nearestWeekday: ['最近的工作日（周一至周五）至本月', '日'],
                    someWeekday: ['在这个月的第', '个'],
                },
                Week: ['天', '一', '二', '三', '四', '五', '六'].map(function (val) {
                    '星期' + val
                }),
                Month: {
                    name: '月',
                    every: '每一月',
                    interval: ['每隔', '月执行 从', '月开始'],
                    specific: '具体月数(可多选)',
                    cycle: ['从', '到', '月之间的每个月']
                },
                Year: {
                    name: '年',
                    every: '每一年',
                    interval: ['每隔', '年执行 从', '年开始'],
                    specific: '具体年份(可多选)',
                    cycle: ['从', '到', '年之间的每一年']
                },
                Save: '保存',
                Close: '关闭'
            },
            zh_TW: {
                Seconds: {
                    name: '秒',
                    every: '每一秒钟',
                    interval: ['每隔', '秒执行 从', '秒开始'],
                    specific: '具体秒数(可多选)',
                    cycle: ['周期从', '到', '秒']
                },
                Minutes: {
                    name: '分',
                    every: '每一分钟',
                    interval: ['每隔', '分执行 从', '分开始'],
                    specific: '具体分钟数(可多选)',
                    cycle: ['周期从', '到', '分']
                },
                Hours: {
                    name: '时',
                    every: '每一小时',
                    interval: ['每隔', '小时执行 从', '小时开始'],
                    specific: '具体小时数(可多选)',
                    cycle: ['周期从', '到', '小时']
                },
                Day: {
                    name: '天',
                    every: '每一天',
                    intervalWeek: ['每隔', '周执行 从', '开始'],
                    intervalDay: ['每隔', '天执行 从', '天开始'],
                    specificWeek: '具体星期几(可多选)',
                    specificDay: '具体天数(可多选)',
                    lastDay: '在这个月的最后一天',
                    lastWeekday: '在这个月的最后一个工作日',
                    lastWeek: ['在这个月的最后一个'],
                    beforeEndMonth: ['在本月底前', '天'],
                    nearestWeekday: ['最近的工作日（周一至周五）至本月', '日'],
                    someWeekday: ['在这个月的第', '个'],
                },
                Week: ['天', '一', '二', '三', '四', '五', '六'].map(function (val) {
                    '星期' + val
                }),
                Month: {
                    name: '月',
                    every: '每一月',
                    interval: ['每隔', '月执行 从', '月开始'],
                    specific: '具体月数(可多选)',
                    cycle: ['从', '到', '月之间的每个月']
                },
                Year: {
                    name: '年',
                    every: '每一年',
                    interval: ['每隔', '年执行 从', '年开始'],
                    specific: '具体年份(可多选)',
                    cycle: ['从', '到', '年之间的每一年']
                },
                Save: '保存',
                Close: '关闭'
            },
            en: {
                Seconds: {
                    name: 'Seconds',
                    every: 'Every second',
                    interval: ['Every', 'second(s) starting at second'],
                    specific: 'Specific second (choose one or many)',
                    cycle: ['Every second between second', 'and second']
                },
                Minutes: {
                    name: 'Minutes',
                    every: 'Every minute',
                    interval: ['Every', 'minute(s) starting at minute'],
                    specific: 'Specific minute (choose one or many)',
                    cycle: ['Every minute between minute', 'and minute']
                },
                Hours: {
                    name: 'Hours',
                    every: 'Every hour',
                    interval: ['Every', 'hour(s) starting at hour'],
                    specific: 'Specific hour (choose one or many)',
                    cycle: ['Every hour between hour', 'and hour']
                },
                Day: {
                    name: 'Day',
                    every: 'Every day',
                    intervalWeek: ['Every', 'day(s) starting on'],
                    intervalDay: ['Every', 'day(s) starting at the', 'of the month'],
                    specificWeek: 'Specific day of week (choose one or many)',
                    specificDay: 'Specific day of month (choose one or many)',
                    lastDay: 'On the last day of the month',
                    lastWeekday: 'On the last weekday of the month',
                    lastWeek: ['On the last', ' of the month'],
                    beforeEndMonth: ['day(s) before the end of the month'],
                    nearestWeekday: ['Nearest weekday (Monday to Friday) to the', 'of the month'],
                    someWeekday: ['On the', 'of the month'],
                },
                Week: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
                Month: {
                    name: 'Month',
                    every: 'Every month',
                    interval: ['Every', 'month(s) starting in'],
                    specific: 'Specific month (choose one or many)',
                    cycle: ['Every month between', 'and']
                },
                Year: {
                    name: 'Year',
                    every: 'Any year',
                    interval: ['Every', 'year(s) starting in'],
                    specific: 'Specific year (choose one or many)',
                    cycle: ['Every year between', 'and']
                },
                Save: 'Save',
                Close: 'Close'
            }
        },
        cb;

    var taskRule = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'task_rule'
            }, win);
        },
        _call: function (tagData) {
            cb = tagData.cb;
        },
        _ready: function () {
            var vue = new Vue({
                el: '#task_rule',
                data: function () {
                    return {
                        second: {
                            cronEvery: '3',
                            incrementStart: '3',
                            incrementIncrement: '5',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: ['0'],
                        },
                        minute: {
                            cronEvery: '',
                            incrementStart: '3',
                            incrementIncrement: '5',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: [],
                        },
                        hour: {
                            cronEvery: '',
                            incrementStart: '3',
                            incrementIncrement: '5',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: [],
                        },
                        day: {
                            cronEvery: '',
                            incrementStart: '1',
                            incrementIncrement: '1',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: [],
                            cronLastSpecificDomDay: 1,
                            cronDaysBeforeEomMinus: '',
                            cronDaysNearestWeekday: '',
                        },
                        week: {
                            cronEvery: '',
                            incrementStart: '1',
                            incrementIncrement: '1',
                            specificSpecific: [],
                            cronNthDayDay: 1,
                            cronNthDayNth: '1',
                        },
                        month: {
                            cronEvery: '',
                            incrementStart: '3',
                            incrementIncrement: '5',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: [],
                        },
                        year: {
                            cronEvery: '',
                            incrementStart: '2017',
                            incrementIncrement: '1',
                            rangeStart: '',
                            rangeEnd: '',
                            specificSpecific: [],
                        },
                        output: {
                            second: '',
                            minute: '',
                            hour: '',
                            day: '',
                            month: '',
                            Week: '',
                            year: '',
                        }
                    }
                },
                watch: {
                    data: function () {
                        this.rest(this.$data);
                    }
                },
                computed: {
                    text: function () {
                        return language[this.lang || 'zh_CN']
                    },
                    secondsText: function () {
                        var seconds = '';
                        var cronEvery = this.second.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                seconds = '*';
                                break;
                            case '2':
                                seconds = this.second.incrementStart + '/' + this.second.incrementIncrement;
                                break;
                            case '3':
                                this.second.specificSpecific.map(function (val) {
                                    seconds += val + ','
                                });
                                seconds = seconds.slice(0, -1);
                                break;
                            case '4':
                                seconds = this.second.rangeStart + '-' + this.second.rangeEnd;
                                break;
                        }
                        return seconds;
                    },
                    minutesText: function () {
                        var minutes = '';
                        var cronEvery = this.minute.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                minutes = '*';
                                break;
                            case '2':
                                minutes = this.minute.incrementStart + '/' + this.minute.incrementIncrement;
                                break;
                            case '3':
                                this.minute.specificSpecific.map(function (val) {
                                    minutes += val + ','
                                });
                                minutes = minutes.slice(0, -1);
                                break;
                            case '4':
                                minutes = this.minute.rangeStart + '-' + this.minute.rangeEnd;
                                break;
                        }
                        return minutes;
                    },
                    hoursText: function () {
                        var hours = '';
                        var cronEvery = this.hour.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                hours = '*';
                                break;
                            case '2':
                                hours = this.hour.incrementStart + '/' + this.hour.incrementIncrement;
                                break;
                            case '3':
                                this.hour.specificSpecific.map(function (val) {
                                    hours += val + ','
                                });
                                hours = hours.slice(0, -1);
                                break;
                            case '4':
                                hours = this.hour.rangeStart + '-' + this.hour.rangeEnd;
                                break;
                        }
                        return hours;
                    },
                    daysText: function () {
                        var days = '';
                        var cronEvery = this.day.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                break;
                            case '2':
                            case '4':
                            case '11':
                                days = '?';
                                break;
                            case '3':
                                days = this.day.incrementStart + '/' + this.day.incrementIncrement;
                                break;
                            case '5':
                                this.day.specificSpecific.map(function (val) {
                                    days += val + ','
                                });
                                days = days.slice(0, -1);
                                break;
                            case '6':
                                days = "L";
                                break;
                            case '7':
                                days = "LW";
                                break;
                            case '8':
                                days = this.day.cronLastSpecificDomDay + 'L';
                                break;
                            case '9':
                                days = 'L-' + this.day.cronDaysBeforeEomMinus;
                                break;
                            case '10':
                                days = this.day.cronDaysNearestWeekday + "W";
                                break
                        }
                        return days;
                    },
                    weeksText: function () {
                        var weeks = '';
                        var cronEvery = this.day.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                            case '3':
                            case '5':
                                weeks = '?';
                                break;
                            case '2':
                                weeks = this.week.incrementStart + '/' + this.week.incrementIncrement;
                                break;
                            case '4':
                                this.week.specificSpecific.map(function (val) {
                                    weeks += val + ','
                                });
                                weeks = weeks.slice(0, -1);
                                break;
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                            case '10':
                                weeks = "?";
                                break;
                            case '11':
                                weeks = this.week.cronNthDayDay + "#" + this.week.cronNthDayNth;
                                break;
                        }
                        return weeks;
                    },
                    monthsText: function () {
                        var months = '';
                        var cronEvery = this.month.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                months = '*';
                                break;
                            case '2':
                                months = this.month.incrementStart + '/' + this.month.incrementIncrement;
                                break;
                            case '3':
                                this.month.specificSpecific.map(function (val) {
                                    months += val + ','
                                });
                                months = months.slice(0, -1);
                                break;
                            case '4':
                                months = this.month.rangeStart + '-' + this.month.rangeEnd;
                                break;
                        }
                        return months;
                    },
                    yearsText: function () {
                        var years = '';
                        var cronEvery = this.year.cronEvery;
                        switch (cronEvery.toString()) {
                            case '1':
                                years = '*';
                                break;
                            case '2':
                                years = this.year.incrementStart + '/' + this.year.incrementIncrement;
                                break;
                            case '3':
                                this.year.specificSpecific.map(function (val) {
                                    years += val + ','
                                });
                                years = years.slice(0, -1);
                                break;
                            case '4':
                                years = this.year.rangeStart + '-' + this.year.rangeEnd;
                                break;
                        }
                        return years;
                    },
                    cron: function () {
                        var secondsText = this.secondsText || '*';
                        var minutesText = this.minutesText || '*';
                        var hoursText = this.hoursText || '*';
                        var daysText = this.daysText || '*';
                        var monthsText = this.monthsText || '*';
                        var weeksText = this.weeksText || '*';
                        var yearsText = this.yearsText || '*';
                        return secondsText + ' ' + minutesText + ' ' + hoursText + ' ' + daysText + ' ' + monthsText + ' ' + weeksText + ' ' + yearsText;
                    }
                },
                methods: {
                    sure: function () {
                        cb(this.cron);
                        self._closeTpl();
                    },
                    cancel: function () {
                        self._closeTpl();
                    },
                    rest: function (data) {
                        for (var i in data) {
                            if (data[i] instanceof Object) {
                                this.rest(data[i])
                            } else {
                                switch (typeof data[i]) {
                                    case 'object':
                                        data[i] = [];
                                        break;
                                    case 'string':
                                        data[i] = '';
                                        break;
                                }
                            }
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('task_rule');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new taskRule();

});